package com.santu.edu.supporter.biz.order.wm.dao;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.santu.edu.supporter.biz.order.common.pojo.enums.OrderStatusEnum;
import com.santu.edu.supporter.biz.order.wm.dao.mapper.WmOrderMapper;
import com.santu.edu.supporter.biz.order.wm.pojo.dto.WmOrderDTO;
import com.santu.edu.supporter.biz.order.wm.pojo.po.WmOrderPO;
import com.santu.edu.supporter.util.PageData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author luoyaoqi
 * @createTime 2024/2/22
 * @description
 */
@Repository
@RequiredArgsConstructor
public class WmOrderDao {

    private final WmOrderMapper wmOrderMapper;

    public void addOrder(WmOrderDTO dto) {
        WmOrderPO item = WmOrderPO.createItem(dto);
        wmOrderMapper.insertOne(item);
    }

    public void updateOrder(WmOrderDTO dto) {
        WmOrderPO item = WmOrderPO.createItem(dto);
        wmOrderMapper.updateOne(item);
    }

    public void cancelOrder(String orderId) {
        wmOrderMapper.cancelOrder(orderId);
    }

    public PageData<WmOrderDTO> getOrderList(String schoolId, String userId, Integer orderType, int page, int size) {
        Page<WmOrderPO> pageData = wmOrderMapper.selectOrderPage(schoolId, userId, orderType, new Page<>(page, size));
        List<WmOrderPO> recordList = pageData.getRecords();
        if (CollUtil.isEmpty(recordList)) {
            return new PageData<>(Collections.emptyList(), pageData.getPages(), pageData.getTotal());
        }
        List<WmOrderDTO> dtoList = recordList.stream().map(WmOrderDTO::createItem).collect(Collectors.toList());
        return new PageData<>(dtoList, pageData.getPages(), pageData.getTotal());
    }

    public void updateOrderStatus(String orderId, OrderStatusEnum orderStatus,OrderStatusEnum nowOrderStatus) {
        wmOrderMapper.updateOrderStatus(orderId, orderStatus.getCode(),nowOrderStatus.getCode());
    }
}
